//
//  HMSpringFlowersPlayMP3Controller.m
//  HowToDo
//
//  Created by qu on 2017/1/5.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMSpringFlowersPlayMP3Controller.h"
#import "MOPopWindow.h"
#import "HMMessageViewController.h"
#import <SVProgressHUD/SVProgressHUD.h>

typedef enum : NSUInteger {
    //btn样式
    HMButtonTypeFont = 1,
    HMButtonTypeLike = 2,
    HMButtonTypeMessage = 3,
    HMButtonTypeShare = 4,
    //字号样式
    HMButtonTypeLittle = 5,
    HMButtonTypeMiddel = 6,
    HMButtonTypeLarge = 7,
    //分享类型
    HMButtonTypeFriends = 8,
    HMButtonTypeWechat = 9,
    
} HMButtonType;

@interface HMSpringFlowersPlayMP3Controller ()
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSMutableArray *btnArrayM;
@property (nonatomic, strong) UIButton *fontBtn;
@property (nonatomic, strong) UIButton *likeBtn;
@property (nonatomic, strong) UIButton *massageBtn;
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, weak) UIView *sharedBackGroundView;
@property (nonatomic, strong) NSMutableArray *btnCacheArray;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, weak) UIView *fontBackGroundView;
@property (nonatomic, strong) NSMutableArray *fontBtnArray;
@property (nonatomic, strong) UILabel *countLabel;

@end

@implementation HMSpringFlowersPlayMP3Controller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [super setupNavRightItemsByOne];
    
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:webView];
    self.webView = webView;
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-50);

    }];
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.offset(0.5);
        make.right.left.equalTo(self.view);
        make.top.equalTo(self.webView.mas_bottom).offset(0);
        
    }];
    self.fontBtn = [self makeBtnWithTitle:@"字号" Image:@"subscribe_icon_text" type:HMButtonTypeFont];
    self.massageBtn= [self makeBtnWithTitle:@"写留言" Image:@"subscribe_icon_input" type:HMButtonTypeMessage];
   self.likeBtn = [self makeBtnWithTitle:@" "Image:@"subscribe_info_like" type:HMButtonTypeLike];
    
    self.shareBtn = [self makeBtnWithTitle:@"请朋友读" Image:@"subscribe_icon_share" type:HMButtonTypeShare];
    
    
    [self.btnArrayM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    [self.btnArrayM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(0);
        make.bottom.equalTo(self.view);
        
    }];
    
    UILabel *countLabel = [[UILabel alloc]init];
    [self.view addSubview:countLabel];
    
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.left.bottom.equalTo(self.likeBtn);
        make.height.offset(20);
    }];
    
    countLabel.text = [NSString stringWithFormat:@"%zd",self.like_count];
    countLabel.textAlignment = NSTextAlignmentCenter;
    countLabel.font = [UIFont systemFontOfSize:12];
    countLabel.textColor = [UIColor colorWithHex:0x929292];
    self.countLabel = countLabel;

    
  
    NSString *URLStr = [NSString stringWithFormat:@"http://howtodo.besttoptoday.com/app/columnDetail?ID=%@",self.urlStrID];
    
    NSURL *url = [NSURL URLWithString:URLStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];

    
}



//批量加载Btn
- (UIButton *)makeBtnWithTitle:(NSString *)title Image:(NSString *)imageName type:(HMButtonType)type {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor colorWithHex:0x929292] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    btn.tag = type;
    
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self setButtonContentType:btn WithSpacing:4.0f];
    
    [self.view addSubview:btn];
    
    [self.btnArrayM addObject:btn];
    
    
    return btn;
}
//点击事件
- (void)buttonClick:(UIButton *)send {
    
    switch (send.tag) {
        case HMButtonTypeFont:
            [self creatFontView];
            break;
        case HMButtonTypeMessage:
        {
            HMMessageViewController *messageVC = [[HMMessageViewController alloc]init];
            [self presentViewController:messageVC animated:YES completion:nil];
        }
            break;
        case HMButtonTypeLike:
        {
           
            self.likeBtn.selected = !self.likeBtn.selected;

            
            if (self.likeBtn.selected) {
                 self.countLabel.text = [NSString stringWithFormat:@"%zd",++self.like_count];
                [self.likeBtn setImage:[UIImage imageNamed:@"subscribe_info_already_like"] forState:UIControlStateNormal];
                
            }else {
                 self.countLabel.text = [NSString stringWithFormat:@"%zd",--self.like_count];
                  [self.likeBtn setImage:[UIImage imageNamed:@"subscribe_info_like"] forState:UIControlStateNormal];
                
            }
        }
            break;
        case HMButtonTypeShare:
            [self creatShareView];
            break;
       
    }

}


- (void)creatShareView {
    
    UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, k_SCREEN_WIDTH, k_SCREEN_HEIGHT)];
    backGroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    [self.view addSubview:backGroundView];
    self.sharedBackGroundView = backGroundView;
    
    UIView *tempView = [[UIView alloc]init];
    tempView.backgroundColor = [UIColor colorWithHex:0xf1f1f1];
    [self.sharedBackGroundView addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.sharedBackGroundView);
        make.height.offset(300);
        
    }];

    
    
    UIButton *friendBtn = [self makeSharedBtnWithTitle:@"朋友圈" Image:@"hot_share_friends" type:HMButtonTypeFriends];
    UIButton *weChatBtn = [self makeSharedBtnWithTitle:@"微信" Image:@"hot_share_wechat" type:HMButtonTypeWechat];
    [self.sharedBackGroundView addSubview:friendBtn];
    [self.sharedBackGroundView addSubview:weChatBtn];
    
    [self.btnCacheArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:80 tailSpacing:80];
    [self.btnCacheArray mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(tempView.mas_top);
        make.height.offset(240);
    }];
    
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sharedBackGroundView addSubview:cancelBtn];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sharedBackGroundView.mas_bottom).offset(-120);
        make.left.right.equalTo(self.sharedBackGroundView);
        make.height.offset(60);
    }];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithHex:0xf9722a] forState:UIControlStateNormal];
    [cancelBtn setBackgroundColor:[UIColor colorWithHex:0xf9f9f9]];
    cancelBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (UIButton *)makeSharedBtnWithTitle:(NSString *)title Image:(NSString *)imageName type:(HMButtonType)type {
    
    UIButton *sharedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sharedBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [sharedBtn setTitle:title forState:UIControlStateNormal];
    
    [sharedBtn setTitleColor:[UIColor colorWithHex:0x565656] forState:UIControlStateNormal];
    sharedBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    sharedBtn.tag = type;
    
    [sharedBtn addTarget:self action:@selector(sharedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self setButtonContentType:sharedBtn WithSpacing:2.0f];
    
    [self.btnCacheArray addObject:sharedBtn];
   
    return sharedBtn;
}
- (void)cancelBtnClick:(UIButton *)send {
    
    [self removeSharedBackView];
    
}

- (void)sharedButtonClick:(UIButton *)btn {
    
    switch (btn.tag) {
        case HMButtonTypeFriends:
            
//            [SVProgressHUD showSuccessWithStatus:@"分享朋友圈成功!"];
//            [SVProgressHUD setMinimumDismissTimeInterval:0.3];
            [[HMNetWorkTool sharedTool]shareTextToPlatformType:2 andShareMessage:@"欢迎使用获得APP"];
            [self removeSharedBackView];
            
            break;
        case HMButtonTypeWechat:
//            [SVProgressHUD showSuccessWithStatus:@"分享微信成功!"];
//            [SVProgressHUD setMinimumDismissTimeInterval:0.3];
            
            [[HMNetWorkTool sharedTool]shareTextToPlatformType:1 andShareMessage:@"欢迎使用获得APP"];
            [self removeSharedBackView];
            
            break;
    }
    
    
}


- (void)creatFontView {
    
    UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, k_SCREEN_WIDTH, k_SCREEN_HEIGHT)];
    backGroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    [self.view addSubview:backGroundView];
    self.fontBackGroundView = backGroundView;
    
    UIView *tempView = [[UIView alloc]init];
    tempView.backgroundColor = [UIColor whiteColor];
    [self.fontBackGroundView addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.fontBackGroundView);
        make.height.offset(200);
        
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [self.fontBackGroundView addSubview:titleLabel];
    titleLabel.text = @"字号选择";
    titleLabel.textColor = [UIColor colorWithHex:0x929292];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(tempView);
        make.height.offset(50);
    }];
    
    UIButton *littleBtn = [self makeFontBtnWithName:@"小" type:HMButtonTypeLittle];
    UIButton *middelBtn = [self makeFontBtnWithName:@"中" type:HMButtonTypeMiddel];
    UIButton *largeBtn = [self makeFontBtnWithName:@"大" type:HMButtonTypeLarge];
    [self.fontBackGroundView addSubview:littleBtn];
    [self.fontBackGroundView addSubview:middelBtn];
    [self.fontBackGroundView  addSubview:largeBtn];
    
    [self.fontBtnArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [self.fontBtnArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(0);
        make.height.offset(80);
    }];
    
    
}
- (UIButton *)makeFontBtnWithName:(NSString *)title type:(HMButtonType)type {
    
    UIButton *fontBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [fontBtn setTitle:title forState:UIControlStateNormal];
    
    [fontBtn setTitleColor:[UIColor colorWithHex:0x929292] forState:UIControlStateNormal];
    fontBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    fontBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    fontBtn.tag = type;
    
    [fontBtn addTarget:self action:@selector(fontButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.fontBtnArray addObject:fontBtn];
    
    return fontBtn;
}
- (void)fontButtonClick:(UIButton *)btn {
    
    [self removeBackView];
    
}

- (void)removeSharedBackView{
    
    [self.btnCacheArray removeAllObjects];
    
    for (NSInteger i = 0; i < self.sharedBackGroundView.subviews.count; i++) {
        
        [[self.sharedBackGroundView.subviews objectAtIndex:i]removeFromSuperview];
        
    }
    [self.sharedBackGroundView removeFromSuperview];
}

- (void)removeBackView{
    
     [self.fontBtnArray removeAllObjects];
    
    for (NSInteger i = 0; i < self.fontBackGroundView.subviews.count; i++) {
        
        [[self.fontBackGroundView.subviews objectAtIndex:i]removeFromSuperview];
        
    }
    [self.fontBackGroundView removeFromSuperview];
}

- (NSMutableArray *)btnCacheArray {
    if (!_btnCacheArray) {
        _btnCacheArray = [NSMutableArray array];
    }
    return _btnCacheArray;
}

- (NSMutableArray *)fontBtnArray {
    if (!_fontBtnArray) {
        _fontBtnArray = [NSMutableArray array];
    }
    return _fontBtnArray;
}

- (NSMutableArray *)btnArrayM {
    
    if (!_btnArrayM) {
        _btnArrayM = [NSMutableArray array];
    }
    return _btnArrayM;
}
//简单做了一下混排 分类里面的 图与字 间距不美观
-(void)setButtonContentType:(UIButton*)setButton WithSpacing:(CGFloat)spacing{
    
    CGFloat imageWith = setButton.imageView.image.size.width;
    CGFloat imageHeight = setButton.imageView.image.size.height;
    CGFloat labelWidth = [setButton.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:setButton.titleLabel.font}].width;
    CGFloat labelHeight = [setButton.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:setButton.titleLabel.font}].height;
    
    //image中心移动的x距离
    CGFloat imageOffsetX = labelWidth / 2 ;
    //image中心移动的y距离
    CGFloat imageOffsetY = labelHeight / 2 + spacing / 2;
    //label中心移动的x距离
    CGFloat labelOffsetX = imageWith/2;
    //label中心移动的y距离
    CGFloat labelOffsetY = imageHeight / 2 + spacing / 2;
    
    setButton.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
    setButton.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
    
}


@end
